var _currentIndex = 0;
//页面初始加载
efform_onload = function() {
	var fdPlanStatus = decodeURI($("#fdPlanStatus").val());
	//隐藏/显示从表按钮
	if(fdPlanStatus != '草拟')
		hideAndShowSubButtons(false);
	else
		hideAndShowSubButtons(true);
}; 
//TAB切换
function changeTab(indexclicked)
{
	var info = new EiInfo();
	info.set("fdProduceplanId",$('#fdProduceplanId').val()); 
	info.set("fdProfessionalplanId",$('#fdProfessionalplanId').val()); 
	info.set("fdPlanYear",$('#_planYear').val()); 
	info.set("fdPlanMonth",$('#_planMonth').val());
	if(indexclicked ==0)
	{  
		EiCommunicator.send("ZMCP15", "query", info, ajax_zc_callback);
	}else if(indexclicked ==1)
	{ 
		EiCommunicator.send("ZMCP16", "query", info, ajax_zk_callback);
	}else if(indexclicked ==2)
	{ 
		EiCommunicator.send("ZMCP17", "query", info, ajax_glaz_callback);
	}  
}

function fundiv(currentIndex,index){  
	_currentIndex=index;
	changeTab(index);
}

/**回调*/
//查询钻尺回调
var ajax_zc_callback = {
	onSuccess : function(eiInfo) {
		efwindow.hide();
		var grid = efgrid.getGridObject("ef_grid_zc");
		grid.refresh(eiInfo);
	},
	onFail : function(eMsg) {
		EFAlert("新增失败，原因：" + eMsg);
	}
}
//查询钻孔回调
var ajax_zk_callback = {
	onSuccess : function(eiInfo) {
		efwindow.hide();
		var grid = efgrid.getGridObject("ef_grid_zk");
		grid.refresh(eiInfo);
	},
	onFail : function(eMsg) {
		EFAlert("新增失败，原因：" + eMsg);
	}
}
//查询管路安装回调
var ajax_glaz_callback = {
	onSuccess : function(eiInfo) {
		efwindow.hide();
		var grid = efgrid.getGridObject("ef_grid_glaz");
		grid.refresh(eiInfo);
	},
	onFail : function(eMsg) {
		EFAlert("新增失败，原因：" + eMsg);
	}
}


/************************************************************************************
 * 钻尺从表操作
 *************************************************************************************/
button_insertdetailzc_onclick = function() {
	if (isSelected("ef_grid_zc") == 0) return;
	efgrid.submitForm("ef_grid_zc", "ZM", "ZMCP15", "insert", true);
} 
button_updatedetailzc_onclick = function() {
	if (isSelected("ef_grid_zc") == 0) return; 
	efgrid.submitForm("ef_grid_zc", "ZM", "ZMCP15", "update", true);
} 
button_deletedetailzc_onclick = function() {
	var grid = isSelected("ef_grid_zc");
	if (grid == 0) return;
	EFConfirm('你确认要删除吗?', '确认框', function(r) {
		if (r) {
			efgrid.submitForm("ef_grid_zc", "ZM", "ZMCP15", "delete", true);
		}
	});
} 
/********************************************************************************
 * 钻孔从表操作
 *********************************************************************************/
button_insertdetailzk_onclick = function() {
	if (isSelected("ef_grid_zk") == 0) {
		return;
	}
	efgrid.submitForm("ef_grid_zk", "ZM", "ZMCP16", "insert", true);
} 
button_updatedetailzk_onclick = function() {
	if (isSelected("ef_grid_zk") == 0) {
		return;
	}
	efgrid.submitForm("ef_grid_zk", "ZM", "ZMCP16", "update", true);
}
button_deletedetailzk_onclick = function() {
	var grid = isSelected("ef_grid_zk");
	if (grid == 0) {
		return;
	}
	EFConfirm('你确认要删除吗?', '确认框', function(r) {
		if (r) {
			efgrid.submitForm("ef_grid_zk", "ZM", "ZMCP16", "delete", true);
		}
	});
} 

/********************************************************************************
 * 管路安装从表操作 
 *********************************************************************************/
button_insertdetailglaz_onclick = function() {
	if (isSelected("ef_grid_glaz") == 0) {
		return;
	}
	efgrid.submitForm("ef_grid_glaz", "ZM", "ZMCP17", "insert", true);
} 
button_updatedetailglaz_onclick = function() {
	if (isSelected("ef_grid_glaz") == 0) {
		return;
	}
	efgrid.submitForm("ef_grid_glaz", "ZM", "ZMCP17", "update", true);
} 
button_deletedetailglaz_onclick = function() {
	var grid = isSelected("ef_grid_glaz");
	if (grid == 0) {
		return;
	}
	EFConfirm('你确认要删除吗?', '确认框', function(r) {
		if (r) {
			efgrid.submitForm("ef_grid_glaz", "ZM", "ZMCP17", "delete", true);
		}
	});
} 
/**
 * 从表按钮显示
 */
var hideAndShowSubButtons = function(status) {

	if (!status) {
		efbutton.setButtonStatus("insertdetailzc", false);
		efbutton.setButtonStatus("updatedetailzc", false);
		efbutton.setButtonStatus("deletedetailzc", false);
		efbutton.setButtonStatus("insertdetailzk", false);
		efbutton.setButtonStatus("updatedetailzk", false);
		efbutton.setButtonStatus("deletedetailzk", false);
		efbutton.setButtonStatus("insertdetailglaz", false);
		efbutton.setButtonStatus("updatedetailglaz", false);
		efbutton.setButtonStatus("deletedetailglaz", false);
	} else {
		efbutton.setButtonStatus("insertdetailzc", true);
		efbutton.setButtonStatus("updatedetailzc", true);
		efbutton.setButtonStatus("deletedetailzc", true);
		efbutton.setButtonStatus("insertdetailzk", true);
		efbutton.setButtonStatus("updatedetailzk", true);
		efbutton.setButtonStatus("deletedetailzk", true);
		efbutton.setButtonStatus("insertdetailglaz", true);
		efbutton.setButtonStatus("updatedetailglaz", true);
		efbutton.setButtonStatus("deletedetailglaz", true);
	}
}
//新增记录行回调函数
efgrid_afterAddNewRow = function(grid_id){ 

	var grid = efgrid.getGridObject(grid_id);
	var rows = grid.getCheckedRows();	
	if(grid_id=="ef_grid_zc")
	{
		for(var i = 0 ; i < rows.length;i++){
			
			grid.setCellValue(rows[i],2,0,TYPE_DATA); 
			grid.setCellValue(rows[i],3,0,TYPE_DATA); 
			grid.setCellValue(rows[i],4,0,TYPE_DATA); 
			grid.setCellValue(rows[i],6,0,TYPE_DATA); 
			grid.setCellValue(rows[i],7,0,TYPE_DATA); 
			grid.setCellValue(rows[i],8,0,TYPE_DATA); 
		}
	}
} 
//提交成功回调函数
efgrid_onAjaxSubmitSuccess = function(gridId, service_name, method_name, eiInfo) {
	if (eiInfo.status != -1) {
		var grid = efgrid.getGridObject(gridId);
		grid.refresh( eiInfo );
	}
	else
	{
		submitCallBackEiMsg = eiInfo.msg;
	}
};

//单元格编辑后回调的方法
efgrid_onDataCellSaved = function( grid_id, row_index, col_index, cell_value ) 
{
	if(grid_id == "ef_grid_zc"){
	  var grid = efgrid.getGridObject( grid_id ); 
	  if( col_index == 6 || col_index == 7 || col_index == 8) 
	  { 
		  var sx = grid.getCellValueByColumnName(row_index,"fdPlanSx");
		  var zx = grid.getCellValueByColumnName(row_index,"fdPlanZx");
		  var xx = grid.getCellValueByColumnName(row_index,"fdPlanXx");
		  grid.setCellValue(row_index,2,Number(sx)+Number(zx)+Number(xx),TYPE_DATA);
		  grid.refreshCell( row_index, 2 , TYPE_DATA ); 
	  } 
	  }
	if(grid_id == "ef_grid_zk"){
		  var grid = efgrid.getGridObject( grid_id ); 
		  if( col_index == 3 || col_index == 4) 
		  { 
			  var sl = grid.getCellValueByColumnName(row_index,"fdHoleLength");//数量
			  var ks = grid.getCellValueByColumnName(row_index,"fdKs");//孔深(米)
			  grid.setCellValue(row_index,5,Number(sl)*Number(ks),TYPE_DATA);
			  grid.refreshCell( row_index, 5 , TYPE_DATA ); 
		  } 
		  }
}